package cn.cnooc.controller;


import cn.hutool.core.collection.CollUtil;
import cn.cnooc.entity.Result;
import cn.cnooc.service.HistoryDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

@RestController
@RequestMapping("/powerGeneration")
@CrossOrigin
public class PowerGenerationController {



    @Autowired
    private HistoryDataService historyDataService;


    @GetMapping("/dayByHourCurve/{date}")
    public Result dayByHourCurve(@PathVariable Long date) {
        Map<String, Object> inverter1 = historyDataService.hourByDayCurve("tb_donggu_inverter1", "yc58");
        Map<String, Object> inverter2 = historyDataService.hourByDayCurve("tb_donggu_inverter2", "yc58");
        List<String> yData1 = (List<String>) inverter1.get("yData");
        List<String> yData2 = (List<String>) inverter2.get("yData");
        List<String> newYdatad = CollUtil.newArrayList();
        IntStream.range(0, yData1.size()).mapToObj(i -> new BigDecimal(yData1.get(i)).add(new BigDecimal(yData2.get(i)))).map(BigDecimal::toString).forEach(newYdatad::add);
        inverter1.put("yData", newYdatad);
        return Result.success(inverter1);

    }
    @GetMapping("/monthByDayCurve/{date}")
    public Result monthByDayCurve(@PathVariable Long date) {
        Map<String, Object> inverter1 = historyDataService.dayByMonthCurve("tb_donggu_inverter1", "yc58");
        Map<String, Object> inverter2 = historyDataService.dayByMonthCurve("tb_donggu_inverter2", "yc58");
        List<String> yData1 = (List<String>) inverter1.get("yData");
        List<String> yData2 = (List<String>) inverter2.get("yData");
        List<String> newYdatad = CollUtil.newArrayList();
        IntStream.range(0, yData1.size()).mapToObj(i -> new BigDecimal(yData1.get(i)).add(new BigDecimal(yData2.get(i)))).map(BigDecimal::toString).forEach(newYdatad::add);
        inverter1.put("yData", newYdatad);
        return Result.success(inverter1);

    }

    @GetMapping("/yearByMonthCurve/{date}")
    public Result yearByMonthCurve(@PathVariable Long date) {
        Map<String, Object> inverter1 = historyDataService.monthByYearCurve("tb_donggu_inverter1", "yc58");
        Map<String, Object> inverter2 = historyDataService.monthByYearCurve("tb_donggu_inverter2", "yc58");
        List<String> yData1 = (List<String>) inverter1.get("yData");
        List<String> yData2 = (List<String>) inverter2.get("yData");
        List<String> newYdatad = CollUtil.newArrayList();
        IntStream.range(0, yData1.size()).mapToObj(i -> new BigDecimal(yData1.get(i)).add(new BigDecimal(yData2.get(i)))).map(BigDecimal::toString).forEach(newYdatad::add);
        inverter1.put("yData", newYdatad);
        return Result.success(inverter1);

    }



}
